﻿window.onload = () => {
    var br = document.createElement("br");
    document.body.appendChild(br);

    var searchParams = new URLSearchParams(window.location.search);
    var name = searchParams.get('_localext_');
    var span = document.createElement("span");
    span.textContent = "ext port=" + name;
    document.body.appendChild(span);

    if (name != null) {
        let port = parseInt(name);
        localexturl = "http://127.0.0.1:" + port;
        testLocalExt(port);
    }
}
var localexturl = "";

async function testLocalExt(port: number) {
    var br = document.createElement("br");
    document.body.appendChild(br);
    var info = await fetch("http://127.0.0.1:" + port + "/info");
    var result = JSON.parse(await info.text());
    var span = document.createElement("span");
    span.textContent = JSON.stringify(result);
    document.body.appendChild(span);


    var br = document.createElement("br");
    document.body.appendChild(br);
    var d = await fetch("http://127.0.0.1:" + port + "/setdebug?value=false");

    var span = document.createElement("span");
    span.textContent = await d.text();;
    document.body.appendChild(span);

    var br = document.createElement("br");
    document.body.appendChild(br);
    var btn = document.createElement("button");
    document.body.appendChild(btn);
    btn.textContent = "test dir";
    btn.onclick = test_dir;

    var br = document.createElement("br");
    document.body.appendChild(br);
    var btn = document.createElement("button");
    document.body.appendChild(btn);
    btn.textContent = "test file";
    btn.onclick = test_file;


    var br = document.createElement("br");
    document.body.appendChild(br);
    var btn = document.createElement("button");
    document.body.appendChild(btn);
    btn.textContent = "test Quit";
    btn.onclick = test_quit;
}
async function test_dir() {
    var info = await fetch(localexturl + "/fileext/getroots");
    var txt = await info.text();
    var json = JSON.parse(txt);
    var mydoc = json["special"]["MyDocuments"] as string;
    addLine(txt);

    var info = await fetch(localexturl + "/fileext/dir_info" + "?path=" + mydoc);
    var txt = await info.text();
    addLine(txt);


    var info = await fetch(localexturl + "/fileext/dir_list" + "?path=" + "D:\\git\\condroid" + "&pattern=*.jpg" + "&recursion=true");
    var txt = await info.text();
    addLine(txt);


    var info = await fetch(localexturl + "/fileext/dir_create" + "?path=" + "D:\\git\\__001");
    var txt = await info.text();
    addLine(txt);

    var info = await fetch(localexturl + "/fileext/dir_create" + "?path=" + "D:\\git\\__002");
    var txt = await info.text();
    addLine(txt);

    var info = await fetch(localexturl + "/fileext/dir_delete" + "?path=" + "D:\\git\\__002");
    var txt = await info.text();
    addLine(txt);

    var info = await fetch(localexturl + "/fileext/dir_create" + "?path=" + "D:\\git\\__003");
    var txt = await info.text();
    addLine(txt);
}
async function test_quit() {
    var info = await fetch(localexturl + "/quit");
    var txt = await info.text();
    addLine(txt);
}
async function test_file() {
    var info = await fetch(localexturl + "/fileext/dir_list" + "?path=" + "D:\\git\\condroid" + "&pattern=*.jpg" + "&recursion=true");
    var txt = await info.text();


    var json = JSON.parse(txt);
    var file = json["files"][0] as string;
    addLine("==find file=" + file);

    var info = await fetch(localexturl + "/fileext/file_info" + "?path=" + file);
    var txt = await info.text();
    addLine("==file_info file=" + txt);

    var info = await fetch(localexturl + "/fileext/file_info" + "?path=" + "d:\\ttt.jpg");
    var txt = await info.text();
    addLine("==file_info ttt file=" + txt);

    //test delete
    var info = await fetch(localexturl + "/fileext/file_delete" + "?path=" + "d:\\ttt.jpg");
    var txt = await info.text();
    addLine("==file_delete ttt file=" + txt);
    var info = await fetch(localexturl + "/fileext/file_info" + "?path=" + "d:\\ttt.jpg");
    var txt = await info.text();
    addLine("==file_info ttt file=" + txt);
    //test copy
    var info = await fetch(localexturl + "/fileext/file_copy" + "?from=" + file + "&to=" + "d:\\ttt.jpg");
    var txt = await info.text();
    addLine("==file_copy=" + txt);
    var info = await fetch(localexturl + "/fileext/file_info" + "?path=" + "d:\\ttt.jpg");
    var txt = await info.text();
    addLine("==file_info ttt file=" + txt);
    //test read
    var re = await fetch(localexturl + "/fileext/file_read" + "?path=" + file + "&format=binary");
    if (re.status != 200) {
        addLine("re=" + re.statusText);
    }
    var f = await re.arrayBuffer();
    addLine("load file len=" + f.byteLength);
    //test write

    var reqop: RequestInit = { "method": "post", "body": f };
    var info = await fetch(localexturl + "/fileext/file_write" + "?path=" + "d:\\ttt2.jpg", reqop);
    var txt = await info.text();
    addLine("==file_write ttt2 file=" + txt);

    //const headers = new Headers();
    //headers.append("Content-Type", "text/plain; charset=utf-8"); // 指定编码为 utf-8

    var reqop: RequestInit = { "method": "post", "body": "very good.地球上的啥子" };//, "headers": headers };
    var info = await fetch(localexturl + "/fileext/file_write" + "?path=" + "d:\\ttt3.info", reqop);
    var txt = await info.text();
    addLine("==file_write ttt3 file=" + txt);

    var formdata = new FormData();
    formdata.append("n1", new Blob(["hasdfd地球上"]), "f1.txt");
    var reqop: RequestInit = { "method": "post", "body": formdata };
    var info = await fetch(localexturl + "/fileext/file_write" + "?path=" + "d:\\ttt4.info", reqop);
    var txt = await info.text();
    addLine("==file_write ttt4 file=" + txt);

    //test read
    var re = await fetch(localexturl + "/fileext/file_read" + "?path=" + "d:\\ttt4.info" + "&format=text");
    var text = await re.text();
    addLine("code=" + re.status + "load text=" + text);
    //test read error
    var re = await fetch(localexturl + "/fileext/file_read" + "?path=" + "d:\\ttt44.info" + "&format=text");
    var text = await re.text();
    addLine("code=" + re.status + "load text=" + text);

}
function addLine(txt: string) {
    var br = document.createElement("br");
    document.body.appendChild(br);
    var span = document.createElement("span");
    span.textContent = txt;
    document.body.appendChild(span);
}